// pages/task/mainpage/mainpage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow: function() {
    //获取我的用户信息
    this.getMyInfo();
    //更新任务信息
    this.getTaskInfo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var shareType;
    var path = '/pages/login/login';
    if (e && e.from == 'button') {
      shareType = e.target.dataset.type;
      if (shareType == 'user') {
        path += '?token=' + this.userShareToken;
      } else if (shareType == 'group') {
        path += '?token=' + this.groupShareToken;
      }
    }
    return {
      title: '【' + this.data.my_info.user_name + '】，0元拼团，1分钱不要，全场包邮>>',
      path: path,
      imageUrl: '../../../style/img/share.png',
      success: () => {
        if (shareType == 'group') {
          app.Ajax({
            method: 'POST',
            url: '/api/index.php?app=home&act=share',
            data: {
              type: shareType
            },
            success: (res) => {
              this.setData({
                message: '分享成功，获得' + res.stars + '星星'
              })
            },
            fail: (err) => {
              this.setData({
                message: '已到达分享上限'
              })
            }
          })
        }
      }
    }
  },

  /**
   * 获取用户信息
   */
  getMyInfo: function() {
    app.getUserInfo(res => {
      this.setData({
        my_info: res
      })
      this.getShareToken();
    })
  },

  /**
   * 获取任务信息
   */
  getTaskInfo: function() {
    app.Ajax({
      url: '/api/task/task_lists',
      data: {
        status: 1
      },
      success: (res) => {
        this.setData({
          daily_task: res.daily_task,
          gzh_task: res.gzh_task,
          xcx_task: res.xcx_task
        })
        // console.log(res)
      }
    })
  },
  /**
   * 查看我的星星
   */
  lookMyStars: function () {
    wx.navigateTo({
      url: '../../my/mystar/mystar',
    })
  },

  /**
   * 查看已完成任务
   */
  viewDoneTask: function() {
    wx.navigateTo({
      url: '../done/done',
    })
  },

  //绑定手机号码
  bindPhone: function () {
    if (!this.data.daily_task.bind_mobile.isCompleted) {
      wx.navigateTo({
        url: '../../others/register/register',
      })
    }
  },

  linkToHome: function (e) {
    let targetId = Number(e.currentTarget.dataset.idx);
    wx.setStorage({
      key: 'homePageTab',
      data: targetId,
    })
    wx.switchTab({
      url: '../../homepage/homepage',
    })
  },

  /**
   * 查看任务详情
   */
  viewTaskInfo: function(e) {
    var taskid = e.currentTarget.dataset.taskid;
    wx.navigateTo({
      url: '../detail/detail?task_id='+ taskid,
    })
  },

  /**
   * 生成分享码
   */
  getShareToken: function () {
    if (this.userShareToken || this.groupShareToken) {
      return;
    }

    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=home&act=token',
      data: {
        type: 0,
        page: 'pages/my/myfriend/friend_list/friend_list?user_id=' + this.data.my_info.user_id,
      },
      success: (res) => {
        this.userShareToken = res.token;
      }
    })
    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=home&act=token',
      data: {
        type: 0,
        page: 'pages/homepage/homepage',
      },
      success: (res) => {
        this.groupShareToken = res.token;
      }
    })
  },
})